package cn.edu.scnu.semart.common.config;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import springfox.documentation.builders.ApiInfoBuilder;
import springfox.documentation.builders.ParameterBuilder;
import springfox.documentation.builders.PathSelectors;
import springfox.documentation.builders.RequestHandlerSelectors;
import springfox.documentation.schema.ModelRef;
import springfox.documentation.service.ApiInfo;
import springfox.documentation.service.Contact;
import springfox.documentation.service.Parameter;
import springfox.documentation.spi.DocumentationType;
import springfox.documentation.spring.web.plugins.Docket;
import springfox.documentation.swagger2.annotations.EnableSwagger2WebMvc;
import java.util.ArrayList;
import java.util.List;
/**
 * Swagger2配置信息
 */
@Configuration
@EnableSwagger2WebMvc
public class Swagger2Config {
    //1
    @Bean
    public Docket webApiConfig(){
        List<Parameter> pars = new ArrayList<>();
        ParameterBuilder tokenPar = new ParameterBuilder();
        tokenPar.name("userId")
                .description("用户token")
                //.defaultValue(JwtHelper.createToken(1L, "admin"))
                .defaultValue("1")
                .modelRef(new ModelRef("string"))
                .parameterType("header")
                .required(false)
                .build();
        pars.add(tokenPar.build());

        Docket webApi = new Docket(DocumentationType.SWAGGER_2)
                .groupName("webApi")
                .apiInfo(webApiInfo())
                .select()
                //只显示api路径下的页面
                // /api/user/login
                // /admin/order/findAll
                // /add/PERSON/all
                .apis(RequestHandlerSelectors.basePackage("cn.edu.scnu.semart"))
//                .paths(PathSelectors.regex("/api/.*"))
                .paths(PathSelectors.any())
                .build()
                .globalOperationParameters(pars);
        return webApi;
    }
    //2
    @Bean
    public Docket adminApiConfig(){
        List<Parameter> pars = new ArrayList<>();
//        ParameterBuilder tokenPar = new ParameterBuilder();
//        tokenPar.name("adminId")
//                .description("用户token")
//                .defaultValue("1")
//                .modelRef(new ModelRef("string"))
//                .parameterType("header")
//                .required(false)
//                .build();
//        pars.add(tokenPar.build());

        Docket adminApi = new Docket(DocumentationType.SWAGGER_2)
                .groupName("adminApi")
                .apiInfo(adminApiInfo())
                .select()
                //只显示admin路径下的页面
                .apis(RequestHandlerSelectors.basePackage("cn.edu.scnu.semart"))
//                .paths(PathSelectors.regex("/admin/.*"))
                .paths(PathSelectors.any())
                .build()
                .globalOperationParameters(pars);
        return adminApi;
    }

    // 3. 专门为商品微服务再建一个分组
    @Bean
    public Docket goodsApiConfig() {
        return new Docket(DocumentationType.SWAGGER_2)
                .groupName("goodsApi")
                .apiInfo(new ApiInfoBuilder()
                        .title("商品服务-API文档")
                        .description("商品微服务接口定义")
                        .version("1.0")
                        .build())
                .select()
                .apis(RequestHandlerSelectors.basePackage("cn.edu.scnu.semart.goods"))
                .paths(PathSelectors.any())
                .build();
    }

    private ApiInfo webApiInfo(){
        return new ApiInfoBuilder()
                .title("网站-API文档")
                .description("本文档描述了尚上优选网站微服务接口定义")
                .version("1.0")
                .contact(new Contact("scnu", "http://scnu.edu.cn", "scnu"))
                .build();
    }
    private ApiInfo adminApiInfo(){
        return new ApiInfoBuilder()
                .title("后台管理系统-API文档")
                .description("本文档描述了尚上优选后台系统服务接口定义")
                .version("1.0")
                .contact(new Contact("scnu", "http://scnu.edu.cn", "scnu"))
                .build();
    }
}